package com.ruyuan.commodity.controller.seller;

import com.ruyuan.commodity.open.api.model.common.WebPage;
import com.ruyuan.commodity.common.mamager.TagsManager;
import com.ruyuan.commodity.open.api.annotation.Auth;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.HashMap;
import java.util.Map;

/**
 * 商品标签控制器
 */
@RestController
@RequestMapping("/seller/goods/tags")
public class GoodsTagsController {

	public static final Logger LOG = LoggerFactory.getLogger(GoodsTagsController.class);

	/**
	 * 商品标签业务管理器
	 */
	@Autowired
	private TagsManager tagsManager;

	@Auth
	@ApiOperation(value	= "查询商品标签列表")
	@ApiImplicitParams({
		 @ApiImplicitParam(name	= "pageNo",	value =	"页码",	required = true, dataType = "int",	paramType =	"query"),
		 @ApiImplicitParam(name	= "pageSize",	value =	"每页显示数量",	required = true, dataType = "int",	paramType =	"query")
	})
	@GetMapping
	public WebPage list(Long pageNo, Long pageSize) {
		return this.tagsManager.list(pageNo, pageSize);
	}

	@Auth
	@ApiOperation(value	= "查询某标签下的商品")
	@ApiImplicitParams({
		 @ApiImplicitParam(name	= "pageNo",	value =	"页码",	required = true, dataType = "int",	paramType =	"query"),
		 @ApiImplicitParam(name	= "pageSize",	value =	"每页显示数量",	required = true, dataType = "int",	paramType =	"query"),
		 @ApiImplicitParam(name	= "tagId",	value =	"标签id",	required = true, dataType = "int",	paramType =	"path")
	})
	@GetMapping("/{tagId}/goods")
	public WebPage listGoods(
			@RequestParam(defaultValue="1", value = "pageNo") Long pageNo,
			@RequestParam(defaultValue="100", value = "pageSize") Long pageSize,
			@PathVariable("tagId") Long tagId) {
		return this.tagsManager.queryTagGoods(tagId, pageNo, pageSize);
	}

	@Auth
	@ApiOperation(value	= "保存指定签下的商品")
	@ApiImplicitParams({
		@ApiImplicitParam(name	= "tagId",	value =	"标签id",	required = true, dataType = "int",	paramType =	"path"),
		@ApiImplicitParam(name	= "goodsIds",	value =	"要保存的商品id",	required = true, dataType = "int",	paramType =	"path"),
	})
	@PutMapping("/{tagId}/goods/{goodsIds}")
	public Map<String, Boolean> saveGoods(@PathVariable("tagId") Long tagId, @PathVariable("goodsIds") Long[] goodsIds) {
		Map<String, Boolean> result = new HashMap<>(1);
		try {
			this.tagsManager.saveTagGoods(tagId,goodsIds);
			result.put("result", true);
			return result;
		} catch (Exception e) {
			LOG.error("保存指定标签下的商品时，发生异常", e);
			result.put("result", false);
			return result;
		}
	}

}
